package httpService

import (
	"github.com/labstack/echo/v4"
	"pm/db"
	"pm/model"
)

 type PackageConfigData struct {
	ID int `json:"ID"`
	Path string `json:"path"`
	Name string `json:"name"`
	Type int    `json:"type"`
	Args string `json:"args"`
	Servers string `json:"servers"`
	LastVersion string `json:"last_version"`
}

func PackageConfigList(c echo.Context)error{
	var list []model.Package
	db.NewDb().Model(&model.Package{}).Find(&list)
	PackageConfigList := make([]PackageConfigData, 0)
	for _,item := range list{
		tmp := PackageConfigData{
			ID: int(item.ID),
			Path: item.Path,
			Name: item.Name,
			Type: item.Type,
			Args: item.Args,
			Servers: item.Servers,
		}
		var packageLast model.PackageHistory
		db.NewDb().Model(&model.PackageHistory{}).Where("name = ?", item.Name).Order("created_at desc").First(&packageLast)
		if packageLast.ID > 0{
			tmp.LastVersion = packageLast.Version
		}
		PackageConfigList =append(PackageConfigList, tmp)

	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"data":PackageConfigList,
	})


}


